<!-- 结算单发票 -->
<template>
  <basic-container>
    <avue-crud
      :option="option"
      :table-loading="loading"
      :data="data"
      :page.sync="page"
      v-model="form"
      ref="crud"
      @search-change="searchChange"
      @search-reset="searchReset"
      @current-change="currentChange"
      @size-change="sizeChange"
      @refresh-change="refreshChange"
      @on-load="onLoad"
      @selection-change="selectionChange"
      @sort-change="sortChange"
    >
      <template slot="submit" slot-scope="{ row }">
        {{ row.submit === 0 ? "未提交" : "已提交" }}
      </template>
      <template slot="menuLeft">
        <el-button
          type="primary"
          :disabled="selection.length === 0"
          size="mini"
          v-if="isAdmin"
          @click.stop="submit"
          >提交</el-button
        >
      </template>
      <template slot="menu" slot-scope="{ row }">
        <el-button
          type="text"
          size="mini"
          icon="el-icon-view"
          @click.stop="viewDetail(row)"
          >明细</el-button
        >
        <el-button
          type="text"
          size="mini"
          icon="el-icon-view"
          @click.stop="view(row)"
          v-if="row.price != '' && row.payStatus != ''"
          >发票明细</el-button
        >
        <el-button
          type="text"
          size="mini"
          icon="el-icon-close"
          @click.stop="close(row)"
          v-if="
            row.fsdPur === 'X' &&
            permission.tms_financial_settlementSheetinvoice_close
          "
          >取消</el-button
        >
        <el-button
          type="text"
          size="mini"
          icon="el-icon-view"
          @click.stop="exportExcel(row)"
          >导出</el-button
        >
      </template>
    </avue-crud>
    <invoiceDetails ref="invoiceDetails" @refresh="refresh" />
    <statementDetails ref="statementDetails" />
  </basic-container>
</template>

<script>
import {
  getSettlementList,
  submitSettlement,
  deleteStatement,
} from "@/api/financial/index";
import { mapGetters } from "vuex";
import invoiceDetails from "./invoiceDetails";
import statementDetails from "./statementDetails";
import { getToken } from "@/util/auth";
export default {
  data() {
    return {
      form: {},
      query: {},
      loading: true,
      page: {
        pageSize: 10,
        currentPage: 1,
        total: 0,
      },
      data: [],
      selection: [],
      selectionDocCurrency: "", //当前选中的币别
    };
  },
  components: {
    invoiceDetails,
    statementDetails,
  },
  computed: {
    ...mapGetters(["permission"]),
    //控制承运商账号不显示
    isAdmin() {
      return this.vaildData(
        this.permission.tms_financial_settlementSheetInvoice_submit,
        false
      );
    },
    option() {
      return {
        height: "auto",
        calcHeight: 30,
        tip: false,
        addBtn: false,
        delBtn: false,
        editBtn: false,
        searchShow: false,
        searchMenuSpan: 6,
        border: true,
        labelWidth: "120",
        menuWidth: "250",
        selection: true,
        selectable: (row) => {
          if (this.selectionDocCurrency === "") {
            return row.price != "" && row.payStatus != "" && row.submit != 1;
          } else {
            return (
              row.price != "" &&
              row.payStatus != "" &&
              row.submit != 1 &&
              row.docCurrency === this.selectionDocCurrency
            );
          }
        },
        column: [
          {
            label: "结算单号",
            prop: "no",
            search: true,
          },
          {
            label: "所属承运商",
            searchLabelWidth: "100",
            prop: "carrierName",
            search: true,
          },
          {
            label: "运费",
            prop: "price",
            sortable: "custom",
          },
          {
            label: "币别",
            prop: "docCurrency",
          },
          {
            label: "合同公司",
            prop: "contractCompany",
          },
          {
            label: "过账状态",
            prop: "payStatus",
            // search: true,
          },
          {
            label: "过账日期",
            prop: "postingDate",
          },
          {
            label: "提交状态",
            prop: "submit",
          },
          {
            label: "过账状态",
            prop: "whetherToPost",
            type: "select",
            search: true,
            searchRange: true,
            editDisplay: false,
            hide: true,
            showColumn: false,
            dicData: [
              {
                label: "未过账",
                value: false,
              },
              {
                label: "已过账",
                value: true,
              },
            ],
          },
          {
            label: "过账日期",
            type: "datetime",
            prop: "createTime",
            search: true,
            searchRange: true,
            editDisplay: false,
            hide: true,
            showColumn: false,
            defaultTime: ["00:00:00", "23:59:59"],
            format: "yyyy-MM-dd HH:mm:ss",
            valueFormat: "yyyy-MM-dd HH:mm:ss",
            startPlaceholder: "请选择开始日期",
            endPlaceholder: "请选择结束日期",
          },
        ],
      };
    },
  },
  methods: {
    searchReset() {
      this.query = {};
      this.onLoad(this.page);
    },
    searchChange(params, done) {
      this.query = params;
      this.page.currentPage = 1;
      this.onLoad(this.page, params);
      done();
    },
    currentChange(currentPage) {
      this.page.currentPage = currentPage;
    },
    sizeChange(pageSize) {
      this.page.pageSize = pageSize;
    },
    refreshChange() {
      this.onLoad(this.page, this.query);
    },
    onLoad(page, params = {}) {
      let query = Object.assign(params, this.query);
      if (query.createTime) {
        query = {
          ...query,
          postingDateBegins: query.createTime ? query.createTime[0] : "",
          endOfPostingDate: query.createTime ? query.createTime[1] : "",
        };
        delete query.createTime;
      }
      this.loading = true;
      getSettlementList(page.currentPage, page.pageSize, query).then((res) => {
        const data = res.data.data;
        this.page.total = data.total;
        this.data = data.records;
        this.loading = false;
        this.$nextTick(() => {
          this.$refs.crud.$refs.table.doLayout();
          this.$refs.crud.clearSort();
        });
      });
    },
    refresh() {
      this.onLoad(this.page);
    },
    view(row) {
      this.$refs.invoiceDetails.open(
        row.no,
        row.submit,
        row.docCurrency,
        row.price
      );
    },
    //明细
    viewDetail(row) {
      this.$refs.statementDetails.open(row.no);
    },
    //取消
    close(row) {
      this.$confirm("是否确定删除该发票?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          deleteStatement({ id: row.id }).then(
            () => {
              this.onLoad(this.page);
              this.$message({
                type: "success",
                message: "操作成功!",
              });
            },
            (error) => {
              window.console.log(error);
            }
          );
        })
        .catch(() => {});
    },
    //多选
    selectionChange(list) {
      if (list.length !== 0) {
        this.selectionDocCurrency = list[0].docCurrency;
      } else {
        this.selectionDocCurrency = "";
      }
      this.selection = list;
      for (let i in list) {
        if (list[i].docCurrency !== this.selectionDocCurrency) {
          this.$refs.crud.$refs.table.toggleRowSelection(list[i], false);
        }
      }
    },
    submit() {
      let no = this.selection
        .map((item) => {
          return item.no;
        })
        .join(",");
      this.$confirm("提交后无法修改，是否确认提交发票明细?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          submitSettlement(no).then(({ data }) => {
            if (data.code === 200 && data.success) {
              this.$message({
                type: "success",
                message: "操作成功!",
              });
              this.refresh();
              this.dialogVisible = false;
            }
          });
        })
        .catch(() => {});
    },
    //排序
    sortChange(val) {
      switch (val.prop) {
        case "price":
          if (val.order === "ascending") {
            this.data = this.data.sort((a, b) => {
              return a.price - b.price;
            });
          } else if (val.order === "descending") {
            this.data = this.data.sort((a, b) => {
              return b.price - a.price;
            });
          } else {
            this.refreshChange();
          }
          break;
      }
    },
    //导出
    exportExcel(row) {
      var url = `/api/blade-tms/billing/Settlement/download?Blade-Auth=bearer ${getToken()}&no=${
        row.no
      }`;
      this.downFile(url);
    },
  },
};
</script>

<style></style>
